package map_set;

import java.util.HashMap;
//290. 单词规律
public class Num290_wordPattern {
    public boolean wordPattern(String pattern, String str) {
        //分割单词
        String [] words = str.split(" ");
        if (words.length != pattern.length())return false;
        HashMap<Character,String> map = new HashMap<>();
        //遍历
        for (int i = 0; i < pattern.length(); i++) {
            //拿出规律
            char ch = pattern.charAt(i);
            if (map.containsKey(ch)){
                //判断重复的key的value是否和之前存放的key的value一致
                if (!map.get(ch).equals(words[i])){
                    return false;
                }
            }else {
                if (map.containsValue(words[i])){
                    return false;
                }

                //放进键值对
                map.put(ch,words[i]);

            }


        }
        return true;


    }


}
